part of 'fragment.dart';

class _PurchaseManagementFragmentWloc
    extends StatefulWloc<_PurchaseManagementFragmentWlocState, PurchaseManagementFragment>
    with SingleTickerProviderStateMixin {
  late final PurchaseManagementFragmentBloc _bloc;

  @override
  _PurchaseManagementFragmentWlocState get initialState {
    return _PurchaseManagementFragmentWlocStateFactory().create(
      blocState: _bloc.state,
    );
  }

  void onWebPurchasedSubscriptionCancellationButtonPressed({
    required String originalTransactionId,
  }) {
    _bloc.requestWebPurchasedSubscriptionCancellation(
      originalTransactionId: originalTransactionId,
    );
  }

  void onWebPurchasedSubscriptionAutoRenewalEnablementButtonPressed({
    required String originalTransactionId,
  }) {
    _bloc.requestWebPurchasedSubscriptionAutoRenewalEnablement(
      originalTransactionId: originalTransactionId,
    );
  }

  String getPurchasedItemIconAssetPath({
    required PurchasedItemModel purchasedItemModel,
  }) {
    if (purchasedItemModel is WebPurchasedItemModel) {
      return 'assets/store_icons/web_icon.png';
    }

    if (purchasedItemModel is AppStorePurchasedItemModel) {
      return 'assets/store_icons/app_store_icon.png';
    }

    if (purchasedItemModel is GooglePlayPurchasedItemModel) {
      return 'assets/store_icons/google_play_icon.png';
    }

    throw UnimplementedError();
  }

  String getPurchasedItemTitle({
    required PurchasedItemModel purchasedItemModel,
  }) {
    if (purchasedItemModel is OneTimePurchasedItemModel) {
      return PurchaseManagementFragmentTranslation.of(context).lifetimePurchasedItemTitle;
    }

    if (purchasedItemModel is PurchasedSubscriptionModel) {
      final purchasedSubscriptionModel = purchasedItemModel;

      switch (purchasedSubscriptionModel.duration) {
        case PurchasedSubscriptionDuration.month:
          return PurchaseManagementFragmentTranslation.of(context).monthPurchasedSubscriptionTitle;
        case PurchasedSubscriptionDuration.quarter:
          return PurchaseManagementFragmentTranslation.of(context)
              .quarterPurchasedSubscriptionTitle;
        case PurchasedSubscriptionDuration.year:
          return PurchaseManagementFragmentTranslation.of(context).yearPurchasedSubscriptionTitle;
      }
    }

    throw UnimplementedError();
  }

  String getWebPurchasedSubscriptionPriceLabel({
    required String price,
    required String currency,
    required PurchasedSubscriptionDuration duration,
  }) {
    final priceWithCurrency =
    PurchaseManagementFragmentTranslation.of(context).buildPurchaseItemPriceWithCurrency(
      price: price,
      currency: currency,
    );

    if (duration == PurchasedSubscriptionDuration.month) {
      return PurchaseManagementFragmentTranslation.of(context).buildMonthlyPriceLabel(
        priceWithCurrency: priceWithCurrency,
      );
    }

    if (duration == PurchasedSubscriptionDuration.year) {
      return PurchaseManagementFragmentTranslation.of(context).buildYearlyPriceLabel(
        priceWithCurrency: priceWithCurrency,
      );
    }

    throw UnimplementedError();
  }

  void _onStateChanged({
    required PurchaseManagementFragmentBlocState blocState,
  }) {
    final updatedState = _PurchaseManagementFragmentWlocStateFactory().create(
      blocState: blocState,
    );

    emit(updatedState);
  }

  @override
  void initState() {
    super.initState();

    _bloc = PurchaseManagementFragmentBloc(
      cancelWebPurchasedSubscription: account_di.sl(),
      enableWebPurchasedSubscriptionAutoRenewal: account_di.sl(),
      getPurchaseManagementData: account_di.sl(),
    );

    _bloc.load();
  }

  @override
  void dispose() {
    _bloc.close();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return BlocListener<PurchaseManagementFragmentBloc, PurchaseManagementFragmentBlocState>(
      bloc: _bloc,
      listener: (context, state) {
        _onStateChanged(
          blocState: state,
        );
      },
      child: _PurchaseManagementFragmentView(
        wloc: this,
      ),
    );
  }
}
